package neolith;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.webkit.WebView;
import java.io.File;

public class Util {

  /**
   * webView不同版本设置不同数据目录
   *
   * @param context {@link Application}
   */
  public static void webViewSameDataDirectory(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
      String processName = getProcessName(context);
      if (!context.getApplicationInfo().packageName.equals(processName)) {
        //判断不等于默认进程名称
        WebView.setDataDirectorySuffix(processName);
      }
    }
  }

  public static String getProcessName(Context context) {
    if (context == null) return null;
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
      if (processInfo.pid == android.os.Process.myPid()) {
        return processInfo.processName;
      }
    }
    return null;
  }

  /**
   * 扫描刷新图片
   *
   * @param context {@link Context}
   * @param image {@link File}
   */
  public static void scannerImage(Context context, File image) {
    //4.4以上采用这样的方式来完成图片刷新
    MediaScannerConnection.scanFile(context, new String[] { image.getPath() },
        new String[] { "image/jpeg" },
        new MediaScannerConnection.OnScanCompletedListener() {
          @Override public void onScanCompleted(String path, Uri uri) {

          }
        }
    );
  }
}
